﻿using LS.DicomViewer.ViewModels;
using System;
using System.Windows;
using System.Windows.Controls;

namespace LS.DicomViewer
{
    /// <summary>
    /// DicomThumbnailView.xaml 的交互逻辑
    /// </summary>
    public partial class DicomThumbnailView : UserControl
    {
        private static readonly DependencyProperty FrameTimeProperty;
        private static readonly DependencyProperty PixelSpacingProperty;

        public double? FrameTime
        {
            get { return (double?)GetValue(FrameTimeProperty); }
            set { SetValue(FrameTimeProperty, value); }
        }

        public double? PixelSpacing
        {
            get { return (double?)GetValue(PixelSpacingProperty); }
            set { SetValue(PixelSpacingProperty, value); }
        }

        static DicomThumbnailView()
        {
            FrameTimeProperty = DependencyProperty.Register(nameof(FrameTime), typeof(double?), typeof(DicomThumbnailView));
            PixelSpacingProperty = DependencyProperty.Register(nameof(PixelSpacing), typeof(double?), typeof(DicomThumbnailView));
        }

        public DicomThumbnailViewModel ViewModel => this.mainGrid.DataContext as DicomThumbnailViewModel;

        public static readonly DependencyProperty DicomFileProperty = DependencyProperty.Register("DicomFile", typeof(string), typeof(DicomThumbnailView), new PropertyMetadata(string.Empty, async (a, e) =>
        {
            var originData = a as DicomThumbnailView;
            var vm = originData.ViewModel;
            if (e.NewValue == null)
            {
                vm.Clear();
            }
            else
            {
                try
                {
                    await vm.Load(e.NewValue?.ToString(), originData.FrameTime, originData.PixelSpacing);
                }
                catch (Exception)
                {
                }
            }
        }));

        public static readonly DependencyProperty DoubleClickShowPreviewProperty = DependencyProperty.Register("DoubleClickShowPreview", typeof(bool), typeof(ImageThumbnailView), new PropertyMetadata(true));

        /// <summary>
        /// 双击时是否显示Dicom文件预览
        /// </summary>
        public bool DoubleClickShowPreview
        {
            get => (bool)GetValue(DoubleClickShowPreviewProperty);
            set => SetValue(DoubleClickShowPreviewProperty, value);
        }

        public Action ShowViewerAction { get; set; }

        /// <summary>
        /// Dicom文件
        /// </summary>
        public string DicomFile { get => (string)this.GetValue(DicomFileProperty); set => this.SetValue(DicomFileProperty, value); }

        public DicomThumbnailView()
        {
            InitializeComponent();
        }

        private void Image_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2 && DoubleClickShowPreview)
            {
                ShowDicomViewer();
            }
        }

        public void ShowDicomViewer()
        {
            if (ShowViewerAction != null)
            {
                ShowViewerAction.Invoke();
            }
            else
            {
                ViewModel.ShowDicomViewerCommand.Execute();
            }
        }
    }
}